<template>
  <!-- 用户管理 -->
  <div class="module_par">
    <el-row class="con" :gutter="20">
      <el-col :span="5">
        <div class="con-left">
          <el-tree :data="treeData" :props="defaultProps" @node-click="handleNodeClick" :expand-on-click-node="false"
            :default-expand-all="false" class="tree_con"></el-tree>
        </div>
      </el-col>
      <el-col :span="19">
        <div class="con-right">
          <div class="con-right-top">
            <el-row :gutter="20">
              <el-col :span="8" class="con-right-tit">
                <span class="tit"></span>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <!-- <el-form label-width="100px">
                <el-col :span="6">
                  <el-form-item label="用户名称">
                    <el-input
                      v-model="searchBox.name"
                      autocomplete="off"
                      size="small"
                      class="searchInp"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="登录账号">
                    <el-input
                      v-model="searchBox.code"
                      autocomplete="off"
                      size="small"
                      class="searchInp"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item>
                    <el-button type="primary" class="searchBtn" size="small" @click="goSearch()">查询</el-button>
                    <el-button
                      type="primary"
                      class="searchBtn reditBtn"
                      size="small"
                      @click="reset()"
                    >重置</el-button>
                    <el-button
                      type="primary"
                      class="searchBtn reditBtn"
                      size="small"
                      @click="addList()"
                    >新增</el-button>
                  </el-form-item>
                </el-col>
              </el-form>-->

              <el-col :span="3" style="float:right;margin-bottom:20px">
                <el-button type="primary" class="searchBtn reditBtn" size="small" @click="addList()" v-if="$requestBtnApi.usergua.addBtn">新增用户</el-button>
              </el-col>

              <el-col :span="3" style="margin-bottom:20px;float:right;">
                <el-button v-if="level =='1'" type="primary" class="searchBtn reditBtn" size="small" @click="addCompany()">新增委外公司</el-button>
              </el-col>
            </el-row>
          </div>
          <div class="con-right-bot">
            <el-table :data="tableData" border tooltip-effect="dark" style="width: 100%" class="tab" height="56vh">
              <el-table-column align="center" type="index" label="#"></el-table-column>
              <el-table-column prop="userName" align="center" label="用户名称"></el-table-column>
              <el-table-column prop="userCode" align="center" label="登录账号"></el-table-column>
              <el-table-column prop="organName" align="center" label="所属机构"></el-table-column>
              <el-table-column prop="tenantName" align="center" label="租户名称"></el-table-column>
              <el-table-column prop="updateUser" align="center" label="修改人"></el-table-column>
              <el-table-column prop="updateTime" align="center" label="修改时间"></el-table-column>
              <el-table-column align="center" label="操作" width="200">
                <template slot-scope="scope">
                  <!-- <a class="table_a" @click="seeData(scope.row)">
                    <i class="el-icon-view"></i>查 看
                  </a>-->
                  <a class="table_a" @click="editData(scope.row)" v-if="$requestBtnApi.usergua.editBtn">
                    <i class="el-icon-edit"></i>编 辑
                  </a>
                  <a class="table_a table_a_left table_a_del" @click="delData(scope.row)" v-if="$requestBtnApi.usergua.delBtn">
                    <i class="el-icon-delete"></i>删 除
                  </a>
                </template>
              </el-table-column>
            </el-table>
            <div class="pageBox">
              <el-pagination v-if="total>0" @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :page-sizes="[5, 10, 15, 420]" :page-size="tableQuery.limit" layout="total, sizes, prev, pager, next, jumper"
                :total="total" class="pag"></el-pagination>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 弹框区域 -->
    <el-dialog :title="diaTit" :visible.sync="dialogSta">
      <el-form :model="dialogData" label-width="100px" :rules="roules" ref="ruleForm">
        <el-row :gutter="20" v-if="diaTit == '新增'">
          <el-col :span="12">
            <el-form-item label="登录账号" prop="code">
              <el-input v-model="dialogData.code" autocomplete="off" size="small" class="searchInp"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="name">
              <el-input v-model="dialogData.name" autocomplete="off" size="small" class="searchInp"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" v-if="diaTit == '新增'">
          <el-col :span="12">
            <el-form-item label="密码" prop="pas">
              <el-input v-model="dialogData.pas" autocomplete="off" size="small" class="searchInp" show-password></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="确认密码" prop="newPas">
              <el-input v-model="dialogData.newPas" autocomplete="off" size="small" class="searchInp" show-password></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="岗位" prop="postSta">
              <el-select v-model="dialogData.postSta" multiple collapse-tags placeholder="请选择" size="small">
                <el-option v-for="item in dialogData.postList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色" prop="roleSta">
              <el-select v-model="dialogData.roleSta" multiple collapse-tags placeholder="请选择" size="small">
                <el-option v-for="item in dialogData.roleList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="所属机构">
              <el-input v-model="dialogData.parentName" autocomplete="off" size="small" class="searchInp" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSta = false" size="small">取 消</el-button>
        <el-button type="primary" @click="diaYes('ruleForm')" size="small" v-loading.fullscreen.lock="fullscreenLoading">确
          定</el-button>
      </div>
    </el-dialog>


    <el-dialog title="新增委外公司" :visible.sync="isAddCompany" :close-on-click-modal="false" @close="cancelAdd('companyForm')">
      <el-form label-width="110px" :model="companyInfo" :rules="rules" ref="companyForm">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="机构编号" prop="code">
              <el-input v-model="companyInfo.code" autocomplete="off" size="small" class="searchInp"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="name">
              <el-input v-model="companyInfo.name" autocomplete="off" size="small" class="searchInp"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="上级机构">
              <el-input v-model="companyInfo.parentName" autocomplete="off" size="small" class="searchInp" disabled></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否启用" prop="state">
              <el-select v-model="companyInfo.state" placeholder="请选择" size="small">
                <el-option v-for="item in states" :key="item.value" :label="item.text" :value="item.value"></el-option>
              </el-select>
              </el-switch>
            </el-form-item>
          </el-col>


        </el-row>


        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker v-model="companyInfo.startTime" type="date" size="small" format="yyyy-MM-dd"
                :picker-options="startOptions" value-format="yyyy-MM-dd" class="searchInp"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime">
              <el-date-picker v-model="companyInfo.endTime" type="date" size="small" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                :picker-options="endOptions" class="searchInp"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-divider content-position="left">对账信息</el-divider>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="对账方式" prop="checkModel">
              <el-select v-model="companyInfo.checkModel" size="small" placeholder="请选择" @change="changeCheckModel">
                <el-option v-for="item in DZOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20" v-if="companyInfo.checkModel == '1'">
          <el-col :span="12">
            <el-form-item label="预付款比率" >
              <el-input v-model="companyInfo.backRateList[0].agencyPrepay" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0"
                max="100" step="1" @input="((val) => {handleInput(val, 0,'agencyPrepay')})" type="number" size="small"
                class="searchInp" >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预付款限额" >
              <el-input-number size="small" v-model="companyInfo.backRateList[0].prepayMax" :step="10" :min="0" class="searchInp">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回款提佣比率" >
              <el-input v-model="companyInfo.backRateList[0].agencyReturnRate" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0"
                max="100" step="1" @input="((val) => {handleInput(val, 0,'agencyReturnRate')})" type="number" size="small"
                class="searchInp" >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <div v-if="companyInfo.checkModel == '2'">
          <el-row :gutter="20" v-for="(item, index) in companyInfo.backRateList" :key="item.key">
            <el-col :span="12">
              <el-form-item label="逾期等级" :prop="'backRateList.'+ index +'.levelName'">
                <el-select v-model="item.levelName" size="small" placeholder="请选择" class="searchInp" disabled>
                  <el-option v-for="level in levelOptions" :key="level.label" :label="level.label" :value="level.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">

              <el-form-item label="提佣比率" :prop="'backRateList.'+ index +'.comRate'">
                <el-input v-model="item.comRate" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0" max="100"
                  step="1" @input="((val) => {handleInput(val, index, 'comRate')})" type="number" size="small" class="searchInp">
                  <template slot="append" width="25px">%</template>
                </el-input>
              </el-form-item>

            </el-col>

            <!--
            <el-col :span="8" v-if="index > 0 ">
              <el-form-item>
                <el-button @click="removeBackRate(item, index)" size="small">移 除</el-button>
              </el-form-item>
            </el-col>
            -->
          </el-row>

          <!-- <el-row :gutter="20" >
            <center>
              <el-button @click="addBackRate" size="small">添 加</el-button>
            </center>
          </el-row> -->

<!--        <el-row :gutter="80" >
          <el-col :span="12">
            <el-form-item label="M1提佣比率" prop="rateM1">
              <el-input v-model="companyInfo.rateM1" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0" max="100"
                step="1" @input="((val) => {handleInput(val,'rateM1')})" type="number" size="small" class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="M2提佣比率" prop="rateM2">
              <el-input v-model="companyInfo.rateM2" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0" max="100"
                step="1" @input="((val) => {handleInput(val,'rateM2')})" type="number" size="small" class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="M3提佣比率" prop="rateM3">
              <el-input v-model="companyInfo.rateM3" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0" max="100"
                step="1" @input="((val) => {handleInput(val,'rateM3')})" type="number" size="small" class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="M4提佣比率" prop="rateM4">
              <el-input v-model="companyInfo.rateM4" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0" max="100"
                step="1" @input="((val) => {handleInput(val,'rateM4')})" type="number" size="small" class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="M5提佣比率" prop="rateM5">
              <el-input v-model="companyInfo.rateM5" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0" max="100"
                step="1" @input="((val) => {handleInput(val,'rateM5')})" type="number" size="small" class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="M6提佣比率" prop="rateM6">
              <el-input v-model="companyInfo.rateM6" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0" max="100"
                step="1" @input="((val) => {handleInput(val,'rateM6')})" type="number" size="small" class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="M6+提佣比率" prop="rateM6Plus">
              <el-input v-model="companyInfo.rateM6Plus" onkeyup="if(value<0)value=0;if(value>100)value=100;" min="0"
                max="100" step="1" @input="((val) => {handleInput(val,'rateM6Plus')})" @blur="valildateM6P" type="number" size="small" class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row> -->

        </div>

        <div v-if="companyInfo.checkModel == '3'">
        <!-- <div class="htllist"  v-for="(item, index) in companyInfo.backRateList" :key="item.key">
        <el-row :gutter="20" >
          <el-col :span="12">
            <el-form-item label="逾期等级" :prop="'backRateList.'+ index +'.levelName'">
              <el-input size="small" v-model="item.levelNameStr"  disabled="disabled" class="searchInp"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回退率标准" :prop="'backRateList.'+ index +'.backStandard'">
              <el-input v-model="item.backStandard" onkeyup="if(value<0)value=0;if(value>100)value=100;" :precision="2"
                min="0" max="100" step="1" @input="((val) => {handleInput(val, index, 'backStandard')})" type="number" size="small"
                class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" >
          <el-col :span="12" >
            <el-form-item label="业务人数" :prop="'backRateList.'+ index +'.peopleNumber'">
              <el-input-number size="small" v-model="item.peopleNumber" :step="1" :max="100" :min="0" class="searchInp"></el-input-number>
            </el-form-item>
          </el-col>

          <el-col :span="12" >
            <el-form-item label="人员单价(元)" :prop="'backRateList.'+ index +'.peoplePrice'">
              <el-input-number size="small" v-model="item.peoplePrice" :step="10" :min="0" class="searchInp">
              </el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left"></el-divider>
        </div>
         -->
        <el-row :gutter="20" >

          <el-col :span="12">
            <el-form-item label="回退率标准" >
              <el-input v-model="companyInfo.backRateList[0].backStandard" onkeyup="if(value<0)value=0;if(value>100)value=100;" :precision="2"
                min="0" max="100" step="1" @input="((val) => {handleInput(val, 0, 'backStandard')})" type="number" size="small"
                class="searchInp">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12" >
            <el-form-item label="业务人数" >
              <el-input-number size="small" v-model="companyInfo.backRateList[0].peopleNumber" :step="1" :min="0" class="searchInp"></el-input-number>
            </el-form-item>
          </el-col>

          <el-col :span="12" >
            <el-form-item label="人员单价(元)" >
              <el-input-number size="small" v-model="companyInfo.backRateList[0].peoplePrice" :step="10" :min="0" class="searchInp">
              </el-input-number>
            </el-form-item>
          </el-col>
        </el-row>



        </div>



        <el-divider content-position="left">分案信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="服务类型" prop="category">
              <el-select v-model="companyInfo.category" size="small" placeholder="请选择">
                <el-option v-for="item in serviceOptions" :key="item.value" :label="item.label" :value="item.value"
                  :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="companyInfo.category=='2'">
            <el-form-item label="服务卡种" prop="category">
              <el-select v-model="companyInfo.caseType" size="small" placeholder="请选择">
                <el-option v-for="item in caseOptions" :key="item.value" :label="item.label" :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24" v-if="companyInfo.category=='2' && companyInfo.caseType!='99'">
            <el-form-item label="逾期等级" prop="acceptLevel" :rules="[{required:true,message:'请选择逾期等级'}]">
              <el-checkbox-group v-model="companyInfo.acceptLevel"  >
                <el-checkbox v-for="level in levelOptions" :key="level.label" :value="level.value" :label="level.label"></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="分案权重" prop="powerValue">
              <el-select v-model="companyInfo.powerValue" placeholder="请选择" size="small">
                <el-option v-for="item in powerOptions" :key="item.value" :label="item.text" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <span style="display: block;color: red;">* 例如有100条委外数据，3家委外公司a、b、c的分按权重分别为5、3、2，则分案数分别为：50、30、20。</span>
          </el-col>
        </el-row>





        <!-- <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="佣金比率" prop="profitMarginStr">
              <el-input v-model="companyInfo.profitMarginStr" onkeyup="if(value<0)value=0;if(value>100)value=100;"
                min="1" max="100" step="1" @input="((val) => {handleInput(val,'profitMarginStr')})" type="number" size="small" class="searchInp">
                <el-button slot="append">%</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd('companyForm')" size="small">取 消</el-button>
        <el-button type="primary" size="small" @click="saveCompany('companyForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    data() {
      var validatePas = (rule, value, callback) => {
        if (!value) {
          return callback(new Error("请再次输入新密码"));
        }
        if (value != this.dialogData.pas) {
          return callback(new Error("两次输入密码不一致"));
        }
        return callback();
      };
      //自定义验证委外机构名称
      const validateComName = (rule, value, callback) => {
        if (value != "") {
          const data = {
            name: this.companyInfo.name,
          };
          this.$requestA.get("/IAM/organ/getCodeOrName", data, (res) => {
            if (res.data.code == 200) {
              callback();
            } else {
              callback(new Error("机构名称已存在，请重新输入"));
            }
          })
        } else {
          callback(new Error("请输入机构名称"));
        }
      };
      //自定义验证委外机构代码
      const validateComCode = (rule, value, callback) => {
        if (value != "") {
          const data = {
            code: this.companyInfo.code,
          };
          this.$requestA.get("/IAM/organ/getCodeOrName", data, (res) => {
            if (res.data.code == 200) {
              callback();
            } else {
              callback(new Error("机构编号已存在，请重新输入"));
            }
          })
        } else {
          callback(new Error("请输入机构编号"));
        }
      };
      return {
        caseOptions: [{
          label: "不限",
          value: "99",
        },{
          label: "通联",
          value: "1",
        }, {
          label: "银数",
          value: "2",
        }],
        powerOptions: this.GLOBAL.powerOptions,
        DZOptions: this.GLOBAL.checkModelOptions,
        serviceOptions: [{
          label: "网贷数据",
          value: "1",
          disabled: true,
        }, {
          label: "信贷数据",
          value: "2",
        }],
        states: [{
          text: "启用",
          value: 1,
        }, {
          text: "禁用",
          value: 2,
        }, ],
        bankCode: "",
        companyInfo: {
          code: "",
          name: "",
          bank: '', //默认总行
          powerValue: 1, //默认分案权重因子
          endTime: '',
          startTime: '',
          category: "2",
          caseType: "99",
          checkModel: "1",
          state: 1,

          acceptLevel: [],
          backRateList: [{
            levelName: "",
            backStandard: "0",
            peopleNumber: "0",
            peoplePrice: "0",
            comRate: "0",
            agencyPrepay: "0",
            prepayMax: "0",
            agencyReturnRate: "0",
          }],
        },
        acceptLevelList: [],
        levelOptions: [
          {
            label: "M1",
            value: "1",
          },
          {
            label: "M2",
            value: "2",
          },
          {
            label: "M3",
            value: "3",
          },
          {
            label: "M4",
            value: "4",
          },
          {
            label: "M5",
            value: "5",
          },
          {
            label: "M6",
            value: "6",
          },
          {
            label: "M6+",
            value: "7",
          },
        ],
        isAddCompany: false,
        startOptions: {
          disabledDate: date => {

            if (this.companyInfo.endTime) {
              if (date.getTime() > new Date(this.companyInfo.endTime).getTime()) {
                return true;
              } else {
                return false;
              }
            } else {
              return false;
            }
            //return date > new Date().setDate(new Date().getDate() - 1)
          }
        },
        endOptions: {
          disabledDate: date => {

            if (this.companyInfo.startTime) {
              if (date.getTime() > new Date(this.companyInfo.startTime).getTime()) {
                return false;
              } else {
                return true;
              }
            } else {
              return false;
            }
            //return date > new Date().setDate(new Date().getDate() - 1)
          }
        },

        rules: {
          name: [{
              required: true,
              message: "请输入机构名称",
              trigger: "blur"
            },
            {
              required: true,
              validator: validateComName,
              trigger: "blur"
            }
          ],
          code: [{
              required: true,
              message: "请输入机构编号",
              trigger: "blur"
            },
            {
              required: true,
              validator: validateComCode,
              trigger: "blur"
            }
          ],
          startTime: [{
            required: true,
            message: "请输入开始日期",
            trigger: "blur"
          }],
          endTime: [{
            required: true,
            message: "请输入结束日期",
            trigger: "blur"
          }],
        },


        form: {
          code: "",
          name: "",
          enable: true
        },
        level: "",
        fullscreenLoading: false,
        tableData: [],
        searchSta: false,
        total: 0,
        tableQuery: {
          limit: 5,
          indexNo: 0,
        },
        searchBox: {
          name: "",
          code: "",
        },
        treeData: [],
        defaultProps: {
          children: "children",
          label: "name",
        },
        tit: "",
        organId: "",
        dialogSta: false,
        dialogData: {
          name: "",
          code: "",
          pas: "",
          newPas: "",
          postList: [],
          postSta: [],
          roleList: [],
          roleSta: [],
          parentName: "",
        },
        newDialogData: null,
        editDialogData: null,
        diaTit: "",
        roules: {
          name: [{
            required: true,
            message: "请输入用户名称",
            trigger: "blur"
          }, {
            required: true,
            validator: (rule, value, callback) => {
              if (value != "") {
                const data = {
                  //userCode: this.dialogData.code,
                  userName: this.dialogData.name,
                };
                this.$requestA.get("/IAM/user/getUserCode", data, (res) => {
                  if (res.data.code == 200) {
                    callback();
                  } else {
                    callback(new Error("用户名称已存在，请重新输入"));
                  }
                })

              } else {
                callback(new Error("请输入用户名称"));
              }
            },
            trigger: "blur"
          }],
          code: [{
            required: true,
            message: "请输入登录账号",
            trigger: "blur"
          }, {
            required: true,
            validator: (rule, value, callback) => {
              if (value != "") {
                const data = {
                  userCode: this.dialogData.code,
                  // userName: this.dialogData.name,
                };
                this.$requestA.get("/IAM/user/getUserCode", data, (res) => {
                  if (res.data.code == 200) {
                    callback();
                  } else {
                    callback(new Error("登录账号已存在，请重新输入"));
                  }
                })

              } else {
                callback(new Error("请输入登录账号"));
              }
            },
            trigger: "blur"
          }],
          pas: [{
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }],
          newPas: [{
            required: true,
            validator: validatePas,
            trigger: "blur"
          }],
          postSta: [{
            required: true,
            message: "请选择岗位",
            trigger: "change"
          }],
          roleSta: [{
            required: true,
            message: "请选择角色",
            trigger: "change"
          }],
        },
      };
    },
    computed: {

    },
    watch:{
      'companyInfo.checkModel'(nValue,oVlaue){
        console.log("companyInfo.caseType   oVlaue=",oVlaue,"------nValue==",nValue);
        if(nValue=="2"){//
          this.companyInfo.caseType = "99";
          this.caseOptions.forEach((item) => {
            console.log("item,,",item);

            if(item.value != "99"){
              this.$set(item,"disabled",true);
              //item.disabled = "true";
            }else{
              this.$set(item,"disabled",false);
            }
            this.caseOptions.splice(this.caseOptions.indexOf(item),1,item);
          });
          //不限卡种时，将逾期等级去除，
          this.companyInfo.acceptLevel = [];

        }else if(nValue=="1"){//固定代理类
          this.caseOptions.forEach((item) => {
            this.companyInfo.caseType = "99";
          	let ttt = this.caseOptions[item];
            if(item.value == "99"){
              this.$set(item,"disabled",true);//禁用卡种的无限
            }else{
              this.$set(item,"disabled",false);
            }
            this.caseOptions.splice(this.caseOptions.indexOf(item),1,item);
          });

        }else {
          this.caseOptions.forEach((item) => {
            this.companyInfo.caseType = "1";
          	let ttt = this.caseOptions[item];
            if(item.value == "99"){
              this.$set(item,"disabled",true);
            }else{
              this.$set(item,"disabled",false);
            }
            this.caseOptions.splice(this.caseOptions.indexOf(item),1,item);
          });
        }
      }
    },
    methods: {
      //添加对账信息标准列表
      addBackRate(){
        this.companyInfo.backRateList.push({
          levelName: "",
          backStandard: "0",
          peopleNumber: "0",
          peoplePrice: "0",
          comRate: "0",
          agencyPrepay: "0",
          prepayMax: "0",
          agencyReturnRate: "0",
        });
      },
      //删除对账信息标准列表
      removeBackRate(item, index){
        console.log("remove  before  this.companyInfo.backRateList==",this.companyInfo.backRateList);
        console.log("item==",item);
        this.index = this.companyInfo.backRateList.indexOf(item);
        if(index !== -1){
          console.log("index==",index);
          this.companyInfo.backRateList.splice(index, 1);
        }
        console.log("remove  end this.companyInfo.backRateList==",this.companyInfo.backRateList);
      },
      // 搜索按钮
      goSearch() {
        this.tableQuery.limit = 5;
        this.tableQuery.indexNo = 0;
        this.searchSta = true;
        this.getTableList();
      },
      // 重置按钮
      reset() {
        this.searchBox.name = "";
        this.searchBox.code = "";
        this.tableQuery.limit = 5;
        this.tableQuery.indexNo = 0;
        this.searchSta = false;
        this.getTableList();
      },
      // 新增按钮
      addList() {
        this.diaTit = "新增";
        this.getOptions();
        console.log(this.dialogData.name);
        this.dialogSta = true;
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields();
          this.dialogData.name = "";
          this.dialogData.code = "";
          this.dialogData.pas = "";
          this.dialogData.newPas = "";
          this.dialogData.postSta = [];
          this.dialogData.roleSta = [];
        });
      },
      // 弹框确定
      diaYes(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.fullscreenLoading = true;
            if (this.diaTit == "新增") {
              const data = {
                userCode: this.dialogData.code,
                password: this.$md5(this.dialogData.pas),
                userName: this.dialogData.name,
                organId: this.organId,
                organName: this.tit,
                tenantCode: this.newDialogData.tenantCode,
                tenantName: this.newDialogData.tenantName,
                isEncryption: "1",
                roleIdList: this.dialogData.roleSta,
                postIdList: this.dialogData.postSta,
              };
              this.$requestA[this.$requestBtnApi.usergua.addType](
                this.$requestBtnApi.usergua.addUrl,
                data,
                (res) => {
                  if (res.code == "200") {
                    this.$message.success("新增成功");
                    this.dialogSta = false;
                    this.getTableList();
                  }
                  this.fullscreenLoading = false;
                }
              );

            } else if (this.diaTit == "编辑") {
              //this.editDialogData.userName = this.dialogData.name;
              //this.editDialogData.userCode = this.dialogData.code;
              this.editDialogData.postIdList = this.dialogData.postSta;
              this.editDialogData.roleIdList = this.dialogData.roleSta;
              this.$requestA[this.$requestBtnApi.usergua.editType](
                this.$requestBtnApi.usergua.editUrl,
                this.editDialogData,
                (res) => {
                  if (res.code == "200") {
                    this.$message.success("编辑成功!");
                    this.dialogSta = false;
                    this.getTableList();
                  }
                  this.fullscreenLoading = false;
                }
              );
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      // 编辑按钮
      editData(val) {
        console.log(val);
        this.editDialogData = val;
        this.getOptions();
        this.getOptionList(val.id);
        this.diaTit = "编辑";
        this.dialogSta = true;
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields();
          this.dialogData.name = val.userName;
          this.dialogData.code = val.userCode;
          this.editDialogData.isEncryption = "1";
        });
      },
      // 获取选中数据
      getOptionList(val) {
        this.dialogData.postSta = [];
        this.dialogData.roleSta = [];
        this.$requestA.get("/IAM/user/" + val, {}, (res) => {
          for (const key in res.data.postList) {
            this.dialogData.postSta.push(res.data.postList[key].id);
          }
          for (const key in res.data.roleList) {
            this.dialogData.roleSta.push(res.data.roleList[key].id);
          }
        });
      },
      // 查看按钮
      seeData(val) {},
      // 删除按钮
      delData(val) {
        this.$confirm("此操作将删除当前用户, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            this.$requestA[this.$requestBtnApi.usergua.delType](
              this.$requestBtnApi.usergua.delUrl + "/" + val.id, {},
              (res) => {
                this.getTableList();
                if (res.code == "200") {
                  this.$message.success("删除成功");
                }
              }
            );
          })
          .catch(() => {});
      },
      handleSizeChange(val) {
        this.tableQuery.limit = val;
        this.tableQuery.indexNo = 0;
        this.getTableList();
      },
      handleCurrentChange(val) {
        this.tableQuery.indexNo = val - 0 - 1;
        this.getTableList();
      },
      handleNodeClick(val) {
        this.newDialogData = val;
        this.bankCode = val.code;

        console.log("this.newDialogData===  ", val);
        this.dialogData.parentName = val.name;
        this.tit = val.name;
        this.organId = val.id;
        this.getTableList();
      },
      // 获取树状数据
      getTreeData() {
        const data = {
          loginCode: localStorage.getItem("loginCode"),
          tenantCode: localStorage.getItem("TENANT_CODE"),
        };
        this.$requestA.get("/IAM/organ/tree", data, (res) => {
          this.treeData = res.data;
          this.organId = this.treeData[0].id;
          this.bankCode = this.treeData[0].code;
          this.newDialogData = this.treeData[0];
          this.tit = this.treeData[0].name;
          this.dialogData.parentName = this.tit;
          this.getTableList();
        });
      },
      // 获取右侧table数据
      getTableList() {
        const data = {
          organId: this.organId,
          size: this.tableQuery.limit,
          page: this.tableQuery.indexNo,
          userCode: this.searchSta ? this.searchBox.code : "",
          userName: this.searchSta ? this.searchBox.name : "",
        };

        this.$requestA[this.$requestBtnApi.usergua.queryType](
          this.$requestBtnApi.usergua.queryUrl,
          data,
          (res) => {
            this.tableData = res.data.page.content;
            this.total = res.data.page.totalElements;
            this.level = res.data.level;
          }
        );
      },
      // 获取岗位   角色
      getOptions() {
        this.$requestA.get(
          "/IAM/tenant/" + this.newDialogData.tenantCode, {},
          (res) => {
            this.dialogData.postList = [];
            this.dialogData.roleList = [];
            for (const key in res.data.postList) {
              this.dialogData.postList.push({
                value: res.data.postList[key].id,
                label: res.data.postList[key].name,
              });
            }
            for (const key in res.data.roleList) {
              this.dialogData.roleList.push({
                value: res.data.roleList[key].id,
                label: res.data.roleList[key].roleName,
              });
            }
          }
        );
      },
      //新增委外公司
      addCompany() {
        this.isAddCompany = true;
        console.log("this.dialogData..", this.dialogData)

        this.$set(this.companyInfo, "bank", this.bankCode);
        this.$set(this.companyInfo, "parentName", this.dialogData.parentName);
        this.$set(this.companyInfo, "parentId", this.organId);
        this.$nextTick(()=>{
          //this.$refs['companyForm'].resetFields();//重置数据
          this.$refs['companyForm'].clearValidate();//仅删除验证提示
          //添加回退率列表初始项
          if(!this.companyInfo.backRateList){
            this.companyInfo.backRateList = [];
            this.addBackRate();
          }
        })
      },
      valildateM6P(){
        this.$refs['companyForm'].validate();
      },
      //保留两位小数
      handleInput(value, index, propName) {
        if (value != '') {
          if (value.indexOf('.') > -1) {
            let vv = value.slice(0, value.indexOf('.') + 3);
            //this.companyInfo.profitMarginStr = value.slice(0, value.indexOf('.') + 3);
            this.$set(this.companyInfo.backRateList[index], propName, vv);
          } else {
            this.$set(this.companyInfo.backRateList[index]);
          }
        }
      },
      //切换对账方式时，清楚字段的验证信息
      changeCheckModel(){

        /* let temF = ["agencyPrepay", "agencyReturnRate", "rateM1", "rateM2", "rateM3", "rateM4", "rateM5", "rateM6", "rateM6Plus",
                     "backRate", "peopleNum", "peoplePrice"];
        this.$refs['companyForm'].clearValidate(temF);//仅删除验证提示 */
        this.companyInfo.backRateList = [];
        if(this.companyInfo.checkModel !== '1' && this.companyInfo.checkModel !== '3') {
          this.levelOptions.forEach((item) => {
            console.log("item,,",item);

            this.companyInfo.backRateList.push({
              levelNameStr: item.label,
              levelName: item.value,
              backStandard: "0",
              peopleNumber: "0",
              peoplePrice: "0",
              comRate: "0",
              agencyPrepay: "0",
              prepayMax: "0",
              agencyReturnRate: "0",
            });
          });
        }else{
          this.companyInfo.backRateList.push({
            levelNameStr: "",
            levelName: "",
            backStandard: "0",
            peopleNumber: "0",
            peoplePrice: "0",
            comRate: "0",
            agencyPrepay: "0",
            prepayMax: "0",
            agencyReturnRate: "0",
          });
        }
      },
      //保存委外公司信息
      saveCompany(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

            this.$set(this.companyInfo, "tenantName", localStorage.getItem('TENANT_NAME')),
            this.$set(this.companyInfo, "tenantCode", localStorage.getItem('TENANT_CODE')),
            this.$set(this.companyInfo, "updateTime", new Date()),
            this.$set(this.companyInfo, "updatePeople", localStorage.getItem('USER_CODE')),
            //this.$set(this.companyInfo, "mechanismId", organId);
            //this.$set(this.companyInfo, "bank", this.companyInfo.parentName);
            this.$set(this.companyInfo, "outsourcingCompany", this.companyInfo.name);

            this.updateRate();

            let options = JSON.parse(JSON.stringify(this.companyInfo));


            let tempArr = [];
            this.levelOptions.forEach((item) => {
              if(this.companyInfo.acceptLevel.indexOf(item.label) !== -1){
                tempArr.push(item.value);
              }
            })
            let ss =tempArr.join(",");
            this.$set(options, "acceptLevel", ss);

            let str = JSON.stringify(this.companyInfo.backRateList);
            this.$set(options, "backRateList", str);

            console.log("this.options===", options);

            this.$requestA.post('/IAM/organAndOutsourcingCompany', options, (res) => {
              if (res.code == "200") {

                this.$message.success("新增委外公司成功!");
                this.isAddCompany = false;

                this.getTreeData();
              }
            });

          }
        });
      },
      //设置不同对账方式的各种比率
      updateRate() {
        if (this.companyInfo.checkModel == "1") {
          let ss1 = ["agencyPrepay", "agencyReturnRate"];
          this.setStrToDouble(ss1);
          let ss2 = ["levelName", "backStandard", "peopleNumber", "peoplePrice", "comRate"];
          this.setNull(ss2);

        } else if (this.companyInfo.checkModel == "2") {
          let ss1 = ["comRate"];
          this.setStrToDouble(ss1);
          let ss2 = ["backStandard", "peopleNumber", "peoplePrice", "agencyPrepay", "agencyReturnRate","prepayMax"];
          this.setNull(ss2);
        } else if (this.companyInfo.checkModel == "3") {
          let ss1 = ["backStandard"];
          this.setStrToDouble(ss1);
          let ss2 = ["agencyPrepay", "agencyReturnRate", "prepayMax"];
          this.setNull(ss2);
        }
      },
      setStrToDouble(props) {
        if (props.length > 0) {
          for (var j = 0, len = props.length; j < len; j++) {
            let prop = props[j]
            console.log(props, "j=", j, ";;prop=", prop);
            this.companyInfo.backRateList.forEach((item, index) => {
              if(item[prop]){
                let rate = this.cal.accDiv(item[prop], 100);
                //let rate = this.cal.accDiv(item[prop], 100);
                this.$set(item, prop, rate);
                this.companyInfo.backRateList.splice(index,1,item);
              }
            })
          }
        }
      },
      setNull(props) {
        if (props.length > 0) {
          for (var j = 0, len = props.length; j < len; j++) {
            let prop = props[j]
            console.log(props, "j=", j, ";;prop=", prop);
            this.companyInfo.backRateList.forEach((item, index) => {
              if(item.hasOwnProperty(prop)){
                this.$delete(item, prop);
                this.companyInfo.backRateList.splice(index,1,item);
              }
            })
          }
        }
      },
      closeCompany(formName) {
        this.$refs[formName].resetFields();
        this.cancelAdd();
      },
      cancelAdd() {
        this.isAddCompany = false;

        this.companyInfo = {
          code: "",
          name: "",
          bank: this.companyInfo.bank, //
          powerValue: 1, //默认分案权重因子
          endTime: '',
          startTime: '',
          category: "2",
          caseType: "99",
          checkModel: "1",
          state: 1,

          acceptLevel: [],
          backRateList: [{
            levelName: "",
            backStandard: "0",
            peopleNumber: "0",
            peoplePrice: "0",
            comRate: "0",
            agencyPrepay: "0",
            prepayMax: "0",
            agencyReturnRate: "0",
          }],
        };
      },
      //百分数转小数
      toPoint(percent) {
        var str = percent.replace("%", "");
        str = this.cal.accDiv(str, 100);
        //str = this.$mathUtil.numDiv(str,100);
        //str = str / 100;
        return str;
      },
    },
    created() {
      this.$route.meta.menuList.map((item) => {
        console.log("this.$requestBtnApi.usergua.queryUrl",this.$requestBtnApi.usergua.queryUrl);
        console.log("item.eleUrl==",item.eleUrl);
        if (item.eleName == "查询") {

          this.$requestBtnApi.usergua.queryUrl = item.eleUrl;
          this.$requestBtnApi.usergua.queryType =
            item.operType == "delete" ? "dele" : item.operType;
          this.$requestBtnApi.usergua.queryBtn = true;
        } else if (item.eleName == "新增") {
          this.$requestBtnApi.usergua.addUrl = item.eleUrl;
          this.$requestBtnApi.usergua.addType =
            item.operType == "delete" ? "dele" : item.operType;
          this.$requestBtnApi.usergua.addBtn = true;
        } else if (item.eleName == "编辑") {
          this.$requestBtnApi.usergua.editUrl = item.eleUrl;
          this.$requestBtnApi.usergua.editType =
            item.operType == "delete" ? "dele" : item.operType;
          this.$requestBtnApi.usergua.editBtn = true;
        } else if (item.eleName == "删除") {
          this.$requestBtnApi.usergua.delUrl = item.eleUrl;
          this.$requestBtnApi.usergua.delType =
            item.operType == "delete" ? "dele" : item.operType;
          this.$requestBtnApi.usergua.delBtn = true;
        }
      });
      // this.getOptions();
      this.getTreeData();
    },
    mounted() {},
  };
</script>
<style>
  /*修改滚动条样式*/
  .el-table__body-wrapper::-webkit-scrollbar {
    width: 6px;
    height: 15px;
  }

  .el-table__body-wrapper::-webkit-scrollbar-track {
    background: rgb(239, 239, 239);
    border-radius: 2px;
  }

  .el-table__body-wrapper::-webkit-scrollbar-thumb {
    background: #bfbfbf;
    border-radius: 10px;
  }

  .el-table__body-wrapper::-webkit-scrollbar-thumb:hover {
    background: #333;
  }

  .el-table__body-wrapper::-webkit-scrollbar-corner {
    background: #179a16;
  }
</style>
<style scoped>
  @import "../../assets/css/base.css";

  .con {
    /* margin-top: 10px;
  padding: 0 24px; */
  }

  .con-left {
    width: 100%;
    height: 73vh;
    background-color: #fff;
    overflow: hidden;
    overflow-y: scroll;
    /* padding: 20px 15px; */
  }

  .tree_con {
    padding: 20px 15px;
  }

  /*修改滚动条样式*/
  .con-left::-webkit-scrollbar {
    width: 6px;
    height: 100%;
  }

  .con-left::-webkit-scrollbar-track {
    background: rgb(239, 239, 239);
    border-radius: 2px;
  }

  .con-left::-webkit-scrollbar-thumb {
    background: #bfbfbf;
    border-radius: 10px;
  }

  .con-left::-webkit-scrollbar-thumb:hover {
    background: #333;
  }

  .con-left::-webkit-scrollbar-corner {
    background: #179a16;
  }

  .con-right {
    width: 100%;
    height: 73vh;
    background-color: #fff;
  }

  .pageBox {
    display: flex;
    width: 100%;
    justify-content: left;
    margin-top: 3px;
  }

  .con-right-top {
    border-bottom: 1px dashed #f0f2f5;
  }

  .con-right-bot {
    padding: 10px;
    max-height: 60vh;
  }

  .con-right-tit {
    padding: 10px 20px;
  }

  .con-right-tit .tit {
    font-size: 20px;
  }

  .tab {
    max-height: 56vh;
  }

  .pag {
    text-align: center;
    margin-top: 1vh;
  }
</style>
